package com.example.dictx;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.dictx.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Button btn_add;
    private DBOpenHelper dbOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbOpenHelper = new DBOpenHelper(MainActivity.this, "dict.db", null, 1);

        final ListView listView = findViewById(R.id.result_listView);  //获取显示结果的ListView
        final EditText etSearch = findViewById(R.id.search_et);          //获取查询内容的编辑框
        Button btnSearch = findViewById(R.id.search_btn);     //获取查询按钮
        btn_add = findViewById(R.id.btn_add);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                startActivity(intent);
            }
        });
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = etSearch.getText().toString().trim();
                Cursor cursor = dbOpenHelper.getReadableDatabase().query("dict",null,
                        "word = ?",
                        new String[]{key}, null, null, null);
                ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
                while (cursor.moveToNext()){
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("word", cursor.getString(1));
                    map.put("explain", cursor.getString(2));
                    result.add(map);
                }

                if (result == null || result.size() == 0) {  //如果数据库中没有数据
                    // 显示提示信息，没有相关记录
                    Toast.makeText(MainActivity.this, "很遗憾，没有相关记录！", Toast.LENGTH_LONG).show();
                } else {
                    // 否则将查询的结果显示到ListView列表中
                    SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, result,
                            R.layout.result_main,
                            new String[]{"word", "explain"}, new int[]{
                            R.id.result_word, R.id.result_interpret});
                    listView.setAdapter(simpleAdapter);
                }
            }
        });

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(dbOpenHelper != null){
            dbOpenHelper.close();
        }
    }
}
